#include <stdio.h>
#include <unistd.h>
#include <sys/types.h>
#include <pwd.h>  
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include <sys/wait.h>

#define SIZE 256

char* argv[SIZE];

// 读取指令
void read_instruction();

int main()
{
    char command[SIZE];
    read_instruction(command);


    int i = 0;
    for (i; argv[i]; i ++ ) printf("%s\n", argv[i]);
    
    pid_t rid = fork();


    if (rid == 0)
    {
        int i = 0;
        for (i; argv[i]; i ++ ) printf("%s\n", argv[i]);

        execvp(argv[0], argv);
        exit(errno);
    }
    else
    {
        int status = 0;
        pid_t wid = waitpid(rid, &status, 0);
        if (wid < 0)
        {
            // ...
        }
    }

    return 0;
}

void read_instruction(char command[])
{
    if (fgets(command, SIZE, stdin) == NULL) 
    {
        perror("fail to read_instruction\n");
        exit(1);
    }
    argv[0] = strtok(command, " ");

    int i = 1;
    while ((argv[i ++ ] = strtok(NULL, " ")) != NULL);

    argv[i] = NULL;
}